package com.example.victory.service;

import com.alibaba.fastjson.JSONObject;
import com.example.victory.config.ServiceError;
import com.example.victory.swagger.handler.AppCustomerOverallAssetApiDelegate;
import com.example.victory.swagger.model.*;
import com.example.victory.utils.JsonUtils;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiRequesFsdp;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiRequesFsdpData;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiResponseFsdp;
import com.cgws.fintech.sdk.common.exception.GlobalException;
import com.example.victory.common.web.ServiceHelper;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AppCustomerOverallAssetService implements AppCustomerOverallAssetApiDelegate{

    @Autowired
    private InnerCustOverallAssetFsdpService innerCustOverallAssetFsdpService;

    @Value("${app.iOS.version.num:106}")
    private Integer iOSVersion;

    @Override
    public ResponseEntity<ApiResponseGetCustomerOverallAsset> getCustomerOverallAsset(ApiRequestCustomerId param) {
        ApiRequestCustomerIdData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        ClientType appPlatform = param.getAppPlatform();
        String appVerName = param.getAppVerName();
        ServiceHelper.checkDataNotNull(appPlatform, "appPlatform");
        ServiceHelper.checkDataNotNull(appVerName, "appVerName");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity;
        if (Objects.equal(appPlatform, ClientType.IOS)) {
            Integer version = versionToNum(appVerName);
            if (version <= iOSVersion) {
                fsdpResponseEntity = innerCustOverallAssetFsdpService.getAssetAllInfo106(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
                ApiResponseGetCustomerOverallAssetData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseGetCustomerOverallAssetData.class);
                return ResponseEntity.ok(new ApiResponseGetCustomerOverallAsset().data(resultEntity));
            }
        }

        fsdpResponseEntity = innerCustOverallAssetFsdpService.getAssetAllInfo(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseGetCustomerOverallAssetData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseGetCustomerOverallAssetData.class);
        return ResponseEntity.ok(new ApiResponseGetCustomerOverallAsset().data(resultEntity));
    }

    private Integer versionToNum(String version) {
        log.info("version: {}", version);
        try {
            Integer integer_ver = Integer.valueOf(version.replace(".", ""));
            log.info("integer_ver: {}", integer_ver);
            return integer_ver;
        } catch (Exception e) {
            throw new GlobalException(ServiceError.APP_VERSION_FORMAT_ERROR);
        }
    }
}
